package puzzle.projecteuler.p100;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.StringUtils;

public class Problem036 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		int s = 0;
		for (int i = 0; i < 1000000; i ++) {
			String a = String.valueOf(i);
			String b = String.valueOf(Integer.toBinaryString(i));
			if (a.equalsIgnoreCase(reverse(a)) && b.equalsIgnoreCase(reverse(b))) {
				System.out.println(i);
				s += i;
			}
		}
		System.out.println("-----------------------------");
		System.out.println(s);
	}

	@SuppressWarnings("unchecked")
	public static String reverse(String a) {
		String[] s = new String[a.length()];
		for (int i = 0; i < s.length; i ++) {
			s[i] = a.substring(i, i+1);
		}
		List tmp = Arrays.asList(s);
		Collections.reverse(tmp);
		return StringUtils.join(tmp, "");
	}
}
